/**
 * author ：Peakmain
 * createTime：2024/11/19
 * mail:2726449200@qq.com
 * describe：
 */

/* eslint-disable */
const [base64, alphabet] = [{} as any, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=']
let lookup: any = null
/**
 * base64加密
 * */
base64.encode = function (s: string) {
  let buffer = base64.toUtf8(s),
    position = -1,
    result = '',
    len = buffer.length,
    nan0, nan1, nan2, enc = [, , ,] as unknown[] as number[];
  while (++position < len) {
    nan0 = buffer[position];
    nan1 = buffer[++position];
    enc[0] = nan0 >> 2;
    enc[1] = ((nan0 & 3) << 4) | (nan1 >> 4);
    if (isNaN(nan1)) {
      enc[2] = enc[3] = 64;
    } else {
      nan2 = buffer[++position];
      enc[2] = ((nan1 & 15) << 2) | (nan2 >> 6);
      enc[3] = (isNaN(nan2)) ? 64 : nan2 & 63;
    }
    result += alphabet[enc[0]] + alphabet[enc[1]] + alphabet[enc[2]] + alphabet[enc[3]];
  }
  return result;
}

/**
 * base64解密
 * */
base64.decode = function (s: string) {
  s = s.replace(/\s/g, '');
  if (s.length % 4) {
    throw new Error('InvalidLengthError: decode failed: The string to be decoded is not the correct length for a base64 encoded string.');
  }

  if (/[^A-Za-z0-9+\/=\s]/g.test(s)) {
    throw new Error('InvalidCharacterError: decode failed: The string contains characters invalid in a base64 encoded string.');
  }

  let buffer = base64.fromUtf8(s),
    position = 0,
    result = '',
    len = buffer.length;

  while (position < len) {
    if (buffer[position] < 128) {
      result += String.fromCharCode(buffer[position++]);
    } else if (buffer[position] > 191 && buffer[position] < 224) {
      result += String.fromCharCode(((buffer[position++] & 31) << 6) | (buffer[position++] & 63));
    } else {
      result += String.fromCharCode(((buffer[position++] & 15) << 12) | ((buffer[position++] & 63) << 6) |
        (buffer[position++] & 63));
    }
  }
  return result;
}

base64.toUtf8 = function (s: string) {
  let [position, len, buffer] = [-1, s.length, [] as number[]]
  let chr: number;
  if (/^[\x00-\x7f]*$/.test(s)) {
    while (++position < len) {
      buffer.push(s.charCodeAt(position))
    }
  } else {
    while (++position < len) {
      chr = s.charCodeAt(position)
      if (chr < 128) {
        buffer.push(chr)
      } else if (chr < 2048) {
        buffer.push((chr >> 6) | 192, (chr & 63) | 128)
      } else {
        buffer.push((chr >> 12) | 224, ((chr >> 6) & 63) | 128, (chr & 63) | 128)
      }
    }
  }
  return buffer
}

base64.fromUtf8 = function (s: string) {
  let position = -1,
    len, buffer = [] as number[],
    enc = [, , ,] as unknown[] as number[];

  if (!lookup) {
    len = alphabet.length;
    lookup = {};
    while (++position < len) {
      lookup[alphabet.charAt(position)] = position;
    }
    position = -1;
  }

  len = s.length;
  while (++position < len) {
    enc[0] = lookup[s.charAt(position)];
    enc[1] = lookup[s.charAt(++position)];
    buffer.push((enc[0] << 2) | (enc[1] >> 4));
    enc[2] = lookup[s.charAt(++position)];
    if (enc[2] === 64) {
      break;
    }
    buffer.push(((enc[1] & 15) << 4) | (enc[2] >> 2));
    enc[3] = lookup[s.charAt(++position)];
    if (enc[3] === 64) {
      break;
    }
    buffer.push(((enc[2] & 3) << 6) | enc[3]);
  }
  return buffer;
}

export default base64;
